package kr.ac.chungbuk;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class IBoardDAO {
	private static SqlMapClient sqlMapper;
	public IBoardDAO() {		
	}
	static {
		try {
			Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
			sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
			System.out.println("sqlMapper 연결 성공:" + sqlMapper);
			reader.close();
		} catch (IOException e) {
			// Fail fast.
			throw new RuntimeException(
					"Something bad happened while building the SqlMapClient instance."
							+ e, e);
		}
	}
	public void insert(BoardDTO boardDTO){
		try {
			sqlMapper.insert("insertBoard", boardDTO);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	public List<BoardDTO> selectAll(){
		try {
			List list =sqlMapper.queryForList("selectAll");
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}return null; 
		 //sqlMapper.queryForObject(arg0, arg1)
		 //sqlMapper.delete(arg0, arg1)
		 //sqlMapper.update( arg0, arg1)
	}
	public BoardDTO selectOne(String title){
		BoardDTO board=null;
		try {
			board = (BoardDTO)sqlMapper.queryForObject("selectOne",title);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return board;
	}
	public void delete(String title){
		try {
			int n=sqlMapper.delete("delete",title);
			System.out.println("delete n="+n);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void  update(BoardDTO board){
		try {
			int n=sqlMapper.update("update",board);
			System.out.println("update n="+n);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/* create table  tt (no integer auto_increment primary key, title varchar(100)); */ 
	
}
